﻿using Axiom.CodeManifold;
using Axiom.CodeManifold.WPF.ViewModels;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace Axiom.CodeManifold.WPF.Views
{
	public partial class ServicManifoldView : UserControl
	{
		public ServicManifoldView()
		{
			InitializeComponent();
		}

		public ServiceManifoldViewModel Model { get { return DataContext as ServiceManifoldViewModel; } }

		private void OnAddingNewRow(object sender, AddingNewItemEventArgs e)
		{

		}

		private void OnLoaded(object sender, RoutedEventArgs e)
		{

		}

		private void OnNew(object sender, RoutedEventArgs e)
		{
			Model.SelectedServiceMethod = new MethodDefinition();
		}

		private void OnRefresh(object sender, RoutedEventArgs e)
		{
			Model.Refresh();
		}

		private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
		{

		}

		private void OnSave(object sender, RoutedEventArgs e)
		{
			Model.AddNewMethod();
		}

		private void OnSelectedMethodChanged(object sender, SelectionChangedEventArgs e)
		{
			try
			{
				if (((object[])(e.AddedItems)).Length == 0 || ((object[])(e.AddedItems))[0] as MethodDefinition == null)
				{
					return;
				}

				var method = ((object[])(e.AddedItems))[0] as MethodDefinition;

				Model.SelectedServiceMethod = method;
			}
			catch (Exception ex)
			{
				Model.Status = new StringReader(ex.Message).ReadLine();
			}
		}

		private void OnToolBarLoaded(object sender, RoutedEventArgs e)
		{
			ToolBar toolBar = sender as ToolBar;
			var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
			if (overflowGrid != null)
			{
				overflowGrid.Visibility = Visibility.Collapsed;
			}
		}

		private void OnUndo(object sender, RoutedEventArgs e)
		{
			Model.Undo();
		}

		private void OnLaunchSolutions(object sender, RoutedEventArgs e)
		{
			Model.LaunchSolutions();
		}

		private void OnFilter(object sender, System.Windows.Input.KeyEventArgs e)
		{

		}

		private void OnFilterClear(object sender, RoutedEventArgs e)
		{

		}

		private void OnSortMembers(object sender, RoutedEventArgs e)
		{
			Model.SortMembers();
		}
	}
}
